package com.mzj.saas.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.haixiaoke.saas.repository.util.GsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * uni-app工具类
 *
 * @Author panda
 * @Date 2023/6/12
 **/
@Slf4j
@Component
public class UniAppUtil {

    @Value(("${uniApp.push.message.url}"))
    public String uniAppPushMessageUrl;


    private static String pushMessageUrl;

    @PostConstruct
    public void init() {
        pushMessageUrl = uniAppPushMessageUrl;
    }

    /**
     * 订单创建时推送订单
     */
    public static final String UNI_APP_ORDER_KEY = "uniApp:createOrder:";


    /**
     * 订单推送
     *
     * @param title        标题
     * @param content      文本内容
     * @param orderId      订单编号
     * @param pushClientId 用户设备id集合
     */
    public static void sendMessage(String title, String content, String orderId, List<String> pushClientId) {
        log.info("消息推送参数:title：{}、content：{}、orderId:{}、设备id：{}", title, content, orderId, GsonUtil.toJson(pushClientId));
        Map<String, Object> map = new HashMap<>();
        map.put("title", title);
        map.put("content", content);
        map.put("orderId", orderId);
        String join = String.join(",", pushClientId);
        map.put("push_clientid", join);
        HttpResponse response = HttpRequest.get(pushMessageUrl)
                .form(map).execute();
        log.info("消息推送响应状态：{}、响应数据：{}", response.getStatus(), response.body());
    }
}
